﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using NetJsWire.JsonRpcHandler.Attributes;
using NetJsWire.Widgets.Renderers.Core;

namespace NetJsWire.Widgets.Renderers.Window
{
    [ClassName( "NetJsWire.BasicDialog" )]
    internal class Window : Widget
    {
        // ReSharper disable InconsistentNaming
        protected string _CLOSED = string.Empty;
        protected string _SELECTIONCHANGE = "{0}.getSelectionModel().on(\"selectionchange\", function(sm) {{ if(sm.getSelected()) {{SendToServer(\"{0}\", \"SelectionChange\", \"{1}\", sm.getSelected().get(\"RI\"), true );}} }}, {0});";
        protected string _SHOWN = "function(w) {{ SendToServer(\"{0}\", \"Shown\", \"{1}\", null);}}";
        protected string _SHOWN_CLIENT_NAME = "show";

        // ReSharper restore InconsistentNaming

        public Window( object baseObject )
            : base( baseObject )
        {
        }

        public override void Render()
        {
            if ( !Rendered )
            {
                var bd = ( Ui.Window.Window ) Base;
                SetAttribute( "isContainer", true );
                SetAttribute( "parent", ClientID );
                ConstructorParameters = "\"" + ClientID + "\"";
                //SetConstructionProperty("parent", ClientID);
                SetConstructionProperty( "id", InternalID );
                SetConstructionProperty( "title", bd.Text );
                //TODO: Check why i need to hack this...
                //SetConstructionProperty( "shadow", false );
                SetConstructionProperty( "autoCreate", true );
                if ( bd.Modal )
                    SetConstructionProperty( "modal", true );
                if ( !bd.Resizable )
                    SetConstructionProperty( "resizable", false );
                if ( bd.MinimizeBox )
                    SetConstructionProperty( "minimizable", true );
                if ( !bd.ControlBox )
                    SetConstructionProperty( "closable", false );
                if ( bd.MaximizeBox )
                    SetConstructionProperty( "maximizable", true );
                if ( bd.Collapsible )
                    SetConstructionProperty( "collapsible", true );
                if ( !string.IsNullOrEmpty( bd.IconClass ) )
                    SetConstructionProperty( "iconCls", bd.IconClass );
                if ( bd.Centered )
                    SetConstructionProperty( "centered", true );
                if ( bd.Parent != null && bd.Parent is Ui.Window.Window )
                {
                    SetConstructionProperty( "renderTo", ( bd.Parent as Ui.Window.Window ).Renderer.ClientID );
                }
            }
            base.Render();
        }
    }
}